package com.eleven.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eleven.pojo.Msg;
import com.eleven.pojo.Projects;
import com.eleven.service.AdminProjectsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
public class Admin_ProjectsController {
	@Autowired
	AdminProjectsService apService;
	
	public AdminProjectsService getApService() {
		return apService;
	}

	public void setApService(AdminProjectsService apService) {
		this.apService = apService;
	}

	
	/**
	 * 查询所有项目列表并分页显示
	 * @param pn
	 * @return
	 */
	@RequestMapping("/getAllProjects")
	@ResponseBody
	public Msg getAllProjects(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		// 引入pageHelper
		// 传入页码和每页显示的数量
		PageHelper.startPage(pn, 6);
		System.out.println(pn);
		List<Projects> projects = apService.getAll();
		// 使用pageInfo封装查询后的结果，只需要将pageInfo交给页面就可以了
		// 封装了详细的分页信息
		PageInfo page = new PageInfo(projects, 5);
		// 5 是连续显示5页的内容
		return Msg.success().add("pageInfo", page);
	}

	/**
	 * 按条件查询项目并分页显示
	 * @param pn
	 * @param projects
	 * @return
	 */
	@RequestMapping("/getProjectsByCondition")
	// 此注解可以自动的把返回的数据转换为字符串。
	@ResponseBody
	public  Msg getProjectsByCondition(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Projects projects) {
	    // 引入pageHelper
		PageHelper.startPage(pn, 6);
		System.out.println(projects.getUser().getUsName());
		System.out.println(projects.getPsName());
		System.out.println(projects.getPsType());
		System.out.println(projects.getPsPstId());
		List<Projects> pr = apService.getAllByContinue(projects);
		PageInfo page = new PageInfo(pr, 5);
		return Msg.success().add("pageInfo", page);	
	}
	
	/**
	 * 根据项目id获取项目信息
	 * @param psid
	 * @return
	 */
	@RequestMapping(value="/getProjectByPsId/{psid}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getProject(@PathVariable("psid") Integer psid) {
		System.out.println(psid);
		List<Projects> project = apService.getProjectById(psid);
		return Msg.success().add("project", project.get(0));	
	}
	
	/**
	 * 查看所有未审核项目
	 * @param pn
	 * @return
	 */
	@RequestMapping("/getProjectsByPsType")
	@ResponseBody
	public Msg getProjectsByPsType(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Integer psType) {
		// 引入pageHelper
		// 传入页码和每页显示的数量
		PageHelper.startPage(pn, 6);
		System.out.println(psType);
		List<Projects> projects = apService.getProjectsBypsType(psType);
		// 使用pageInfo封装查询后的结果，只需要将pageInfo交给页面就可以了
		// 封装了详细的分页信息
		PageInfo page = new PageInfo(projects, 5);
		// 5 是连续显示5页的内容
		return Msg.success().add("pageInfo", page);
	}
}
